{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf24f6e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数，over退出：432\n",
      "请输入一个整数，over退出：543\n",
      "请输入一个整数，over退出：645\n",
      "请输入一个整数，over退出：42\n",
      "请输入一个整数，over退出：over\n",
      "sum= 1662\n"
     ]
    }
   ],
   "source": [
    "# 1. 输入n个整数，以over结束，求这n个数之和\n",
    "sum = 0\n",
    "while True:\n",
    "    print(\"请输入一个整数，over退出：\", end='')\n",
    "    num = input()\n",
    "    print(num)\n",
    "    if num == 'over':\n",
    "        break\n",
    "    sum += int(num)\n",
    "print(\"sum=\", sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2199ff6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 将上一题的输入数据放到列表中，统计个数、总和及平均值\n",
    "numbers = list()\n",
    "while True:\n",
    "    print(\"请输入一个整数，over退出：\", end='')\n",
    "    # num = input()\n",
    "    print(num)\n",
    "    if num == 'over':\n",
    "        break\n",
    "    numbers.append(int(num))\n",
    "    sum += int(num)\n",
    "print(\"sum=\", sum)\n",
    "print(\"count=\", len(numbers))\n",
    "print(\"avg=\", sum / len(numbers))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f7e2d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入若干个空格分隔的浮点数，保存到列表并输出和\n",
    "print(\"请输入一组数据，以空格分隔：\", end='')\n",
    "ipt = input().split()\n",
    "numbers = [float(x) for x in ipt]\n",
    "equation = \"\"\n",
    "for i, num in enumerate(numbers):\n",
    "    if num < 0:\n",
    "        s = f'({num})'\n",
    "    else:\n",
    "        s = str(num)\n",
    "    if i < len(numbers) - 1:\n",
    "        equation += s + '+'\n",
    "    else:\n",
    "        equation += s + '='\n",
    "rst = sum(numbers)\n",
    "print(equation, rst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "116ca2e1",
   "metadata": {},
   "source": [
    "**<font color='red'>这道题要仔细看！！</font>**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9361ac7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "ipt = input(\"请输入一组数据，以空格分隔：\").split()\n",
    "numbers = [float(x) for x in ipt]\n",
    "equation = \"\"\n",
    "for num in numbers:\n",
    "    if num >= 0:\n",
    "        equation += f\"{num}+\"\n",
    "    else:\n",
    "        equation += f\"({num})+\"\n",
    "print(equation[:-1], \"=\", sum(numbers)) # 过滤最后多余的加号，直接用字符串切片，天才！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c74d0da1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入若干个整数，数据是有重复的，求出现次数最高的整数\n",
    "print(\"请输入一组整数，以逗号分隔：\")\n",
    "ipt = input().split(',')\n",
    "numbers = [int(x) for x in ipt] # 转化为整数，列表\n",
    "dic = dict()\n",
    "max_count = 1\n",
    "max_num = 0\n",
    "for num in numbers:\n",
    "    dic[num] = numbers.count(num)\n",
    "    if dic[num] > max_count:\n",
    "        max_count = dic[num]\n",
    "        max_num = num\n",
    "    else:\n",
    "        continue\n",
    "print(max_num, max_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "694371f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入若干个整数，数据是有重复的，求出现次数最高的整数\n",
    "print(\"请输入一组整数，以逗号分隔：\")\n",
    "ipt = input().split(',')\n",
    "numbers = [int(x) for x in ipt] # 转化为整数，列表\n",
    "dic = dict()\n",
    "# max_count = 1\n",
    "# max_num = 0\n",
    "for num in numbers:\n",
    "    dic[num] = numbers.count(num)\n",
    "    # if dic[num] > max_count:\n",
    "    #     max_count = dic[num]\n",
    "    #     max_num = num\n",
    "    # else:\n",
    "    #     continue\n",
    "for k in dic.keys():\n",
    "    if dic[k] == max(dic.values()):\n",
    "        print(k)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "783facb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入若干个整数，数据是有重复的，求出现次数最高的整数\n",
    "# 若出现次数最高的数不止一个\n",
    "print(\"请输入一组整数，以逗号分隔：\")\n",
    "ipt = input().split(',')\n",
    "numbers = [int(x) for x in ipt] # 转化为整数，列表\n",
    "dic = dict()\n",
    "max_count = 1\n",
    "max_num = set()\n",
    "for num in numbers:\n",
    "    dic[num] = numbers.count(num)\n",
    "    if dic[num] >= max_count:\n",
    "        max_count = dic[num]\n",
    "        max_num.add(num)\n",
    "    else:\n",
    "        continue\n",
    "print(max_num, max_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c4b8e2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入一组数据，找出最大值和最小值，将最大值与最后一个数据交换位置，将最小值与第一个数据交换位置，其他数据不变\n",
    "print(\"请输入一组数据，以逗号分隔：\", end='')\n",
    "ipt = input().split(',')\n",
    "numbers = [int(x) for x in ipt]\n",
    "\n",
    "max_num = max(numbers)\n",
    "min_num = min(numbers)\n",
    "\n",
    "numbers_new = numbers.copy()\n",
    "numbers_new[0] = min_num\n",
    "numbers_new[-1] = max_num\n",
    "\n",
    "for i in range(len(numbers)):\n",
    "    if numbers[i] == max_num:\n",
    "        numbers_new[i] = numbers[-1]\n",
    "    elif numbers[i] == min_num:\n",
    "        numbers_new[i] = numbers[0]\n",
    "\n",
    "print(' '.join(numbers_new))\n",
    "\n",
    "'''\n",
    "请输入一组数据，以逗号分隔：34,73,17,39,5423,1\n",
    "[1, 73, 17, 39, 1, 34]\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "785c5144",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入一组数据，找出最大值和最小值，将最大值与最后一个数据交换位置，将最小值与第一个数据交换位置，其他数据不变\n",
    "ipt = input(\"请输入一组数据，以逗号分隔：\").split(',')\n",
    "numbers = [int(x) for x in ipt]\n",
    "# numbers = numbers.copy()\n",
    "\n",
    "max_idx = numbers.index(max(numbers))\n",
    "min_idx = numbers.index(min(numbers))\n",
    "\n",
    "numbers[0], numbers[min_idx] = numbers[min_idx], numbers[0]\n",
    "numbers[-1], numbers[max_idx] = numbers[max_idx], numbers[-1] # 注意必须要用已经更改过的列表进行操作，否则之前已改变的顺序不会被保存，就会出现上一个代码块那样的错误\n",
    "\n",
    "print(numbers)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
